package controllers;

import java.util.LinkedList;
import java.util.List;

import play.libs.OAuth2;
import play.libs.WS;
import play.mvc.Controller;
import play.mvc.With;
import play.mvc.results.Redirect;

import com.google.gson.JsonObject;

import controllers.Secure.Security;

import models.*;

@With(Secure.class)
public class Application extends Controller
{
	/**
	 * This method redirects the user to the home page of Online Profile Manager.
	 * 
	 * @author Rania Said
	 */
	public static void index()
	{
		homePage();
	}

	/**
	 * This method renders the home page of Online Profile Manager
	 * 
	 * @author Rania Said
	 */
	public static void homePage()
	{
		System.out.println("Running in Application.homePage method");

		User u = Users.getCurrentUser();
		if (u == null)
		{
			try
			{
				Secure.login();
			}
			catch (Throwable e)
			{

			}
		}
		else
		{
			if (u.abstractProfileList == null)
			{
				u.abstractProfileList = new LinkedList<AbstractProfile>();
				System.out
						.println("The system created a linkedlist of abstract profiles for the user: "
								+ u.username);
			}
			String username = u.username;
			int num = u.abstractProfileList.size();
			render(username, num);
		}
	}

	/*
	 * public static void index() {
	 * System.out.println("Running in Application.index method"); User u =
	 * Users.getCurrentUser(); if (u.profileList == null) { u.profileList = new
	 * LinkedList<SocialProfile>();
	 * System.out.println("created linkedlist of profiles"); }
	 * List<SocialProfile> profileList = u.profileList; List<String> stringList
	 * = new LinkedList<String>(); List<String> srcList = new
	 * LinkedList<String>(); if (u != null) {
	 * System.out.println("The user is not null"); for (int i = 0; i <
	 * profileList.size(); i++) {
	 * System.out.println("looping over the social accounts " + i); if
	 * (!profileList.get(i).access_token.isEmpty()) { JsonObject me = WS
	 * .url("https://graph.facebook.com/me?access_token=%s",
	 * WS.encode(profileList.get(i).access_token))
	 * .get().getJson().getAsJsonObject(); String name =
	 * me.get("name").getAsString(); stringList.add(name); String id =
	 * me.get("id").getAsString(); String src = "https://graph.facebook.com/" +
	 * id + "/picture"; srcList.add(src); } } }
	 * System.out.println(stringList.toString()); int size = srcList.size();
	 * render(stringList, srcList, size); } public static void manageStatus() {
	 * User u = Users.getCurrentUser(); List<SocialProfile> profileList =
	 * u.profileList; if (u != null) {
	 * System.out.println("The user is not null"); for (int i = 0; i <
	 * profileList.size(); i++) {
	 * System.out.println("looping over the social accounts " + i); if
	 * (!profileList.get(i).access_token.isEmpty()) { String tempURL =
	 * "https://graph.facebook.com/" + profileList.get(i).uid +
	 * "/statuses?access_token=" + WS.encode(profileList.get(i).access_token);
	 * JsonObject me = WS.url(tempURL).get().getJson() .getAsJsonObject();
	 * System.out.println("STATUS"); System.out.println(me.toString()); } } }
	 * render(); } public static void auth() {
	 * System.out.println("Running in Application.auth method"); if
	 * (OAuth2.isCodeResponse()) { User u = Users.getCurrentUser(); // the user
	 * currently logged in OAuth2.Response response =
	 * FACEBOOK.retrieveAccessToken(authURL()); u.profileList.add(new
	 * SocialProfile(response.accessToken)); u.save();
	 * System.out.println("Profile List size " + u.profileList.size());
	 * JsonObject me = WS .url("https://graph.facebook.com/me?access_token=%s",
	 * WS.encode(u.profileList.get(u.profileList.size() - 1).access_token))
	 * .get().getJson().getAsJsonObject(); long tempID =
	 * me.get("id").getAsLong(); u.profileList.get(u.profileList.size() - 1).uid
	 * = tempID; String gender = me.get("gender").getAsString();
	 * u.profileList.get(u.profileList.size() - 1).gender = gender;
	 * System.out.println("GENDER" + u.profileList.get(u.profileList.size() -
	 * 1).gender); u.save();
	 * System.out.println("new profile added in Profile List");
	 * System.out.println("RENDERING INDEX"); index(); } String RedirectURL =
	 * "https://www.facebook.com/dialog/oauth?" + "client_id=" +
	 * "388171657886328" + "&redirect_uri=" + authURL() + "&scope=" +
	 * "user_status,user_photos,friends_status,user_checkins";
	 * System.out.println("RedirectURL" + RedirectURL); throw new
	 * Redirect(RedirectURL); } public static String authURL() {
	 * System.out.println("Running in Application.authURL method"); return
	 * play.mvc.Router.getFullUrl("Application.auth"); }
	 */
}
